<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <button type="button" id="importUsers" value="批量导入用户信息" style="display: none"/>
                <form class="layui-form layui-form-pane" action="${path}/user/admin/getUserInfo">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">手机号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="phone" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>


        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" id="add" lay-event="add">批量导入用户</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <div id="test"></div>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>

</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>

<script type="text/html" id="switchTpl">
    <!-- 这里的 checked 的状态只是演示 -->
    <input type="checkbox" name="sex" value="{{d.sex}}" lay-skin="switch" lay-text="女|男" lay-filter="sexDemo"
           {{d.sex==0?'checked':''}}>

</script>

<script>
    layui.use(['form', 'table','layer','upload'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table
            ,upload = layui.upload
            ,layer = layui.layer;

        form.render();
        table.render({
            elem: '#currentTableId',
            url: '${path}/user/admin/getUserInfo',
            height: 650,
            cellMinWidth: 80,
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '用户信息表',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 150, title: 'ID', sort: true, align: "center"},
                {field: 'username', width: 150, title: '用户名', align: "center"},
                {field: 'password', width: 150, title: '密码', align: "center"},
                {field: 'name', width: 150, title: '姓名', align: "center"},
                {field: 'sex', width: 120, title: '性别', templet: '#switchTpl', unresize: true, align: "center"},
                {field: 'age', width: 120, title: '年龄', align: "center"},
                {field: 'phone', width: 180, title: '电话', align: "center"},
                {field: 'email', width: 180, title: '邮箱', align: "center"},
                {field: 'identity', width: 120, title: '会员', align: "center",edit: 'text'},
                {field: 'power', width: 80, title: '身份', align: "center"},
                {title: '操作', minWidth: 120, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line',
            done:function (){
                $("[data-field='identity']").children().each(function(){
                    if($(this).text()=='1'){
                        $(this).text("是")
                    }else if($(this).text()=='0'){
                        $(this).text("否")
                    }
                });
                $("[data-field='power']").children().each(function(){
                    if($(this).text()=='1'){
                        $(this).text("管理员")
                    }else if($(this).text()=='0'){
                        $(this).text("用户")
                    }
                });
            }
        });

        //监听性别操作
        form.on('switch(sexDemo)', function (obj) {
            layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            let result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');
            return false;
        });



        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });


        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                console.log(data)
                let i = layer.open({
                    title: '编辑用户信息',
                    type: 2,
                    shade: 0.2,
                    shadeClose: true,
                    area: ['700px', '600px'],
                    content: 'edit.jsp',
                    success:function(layero, index) {
                        let iframe = window['layui-layer-iframe' + index];
                        iframe.child(data)
                    }
                });
                $(window).on("resize", function () {
                    layer.full(i);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除该用户么',{icon:2, title:'提示'}, function (index) {
                    $.post('${path}/user/admin/delUserById',{"id":data.id},function (data){
                        if(data.code=="200"){
                            layer.msg('删除成功！', {icon:1});
                            table.reload("currentTableId",{
                                page:{
                                    curr: $(".layui-laypage-em").next().html()
                                }
                            });
                             // obj.del();
                        }else {
                            layer.msg('删除失败！', {icon:2});
                        }
                    })
                    layer.close(index);
                });
            }
        });

        //监听单元格编辑
        table.on('edit(currentTableFilter)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
        });





        upload.render({
            elem: '#importUsers'
            ,accept: 'file'
            ,url: '${path}/excel/importUsers' //改成您自己的上传接口
            ,exts: 'xlsx|xls'
            ,done: function(res){
                if(res.code == "200"){
                    layer.msg(res.msg);
                }else {
                    layer.msg(res.msg);
                }
                table.reload("currentTableId",{
                    page:{
                        curr: $(".layui-laypage-em").next().html()
                    }
                });
            }
        });

        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                $("#importUsers").click();
            }
        });
    });





</script>
</body>
</html>